package com.stylefeng.guns.rest.modular.wz.api.v3;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.common.util.StringUtils;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.*;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.IWzMapActivityService;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.WzAdminServiceImplV3;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.controller.AsyncService;
import com.stylefeng.guns.rest.modular.wz.controller.WzFinanceService;
import com.stylefeng.guns.rest.modular.wz.service.IWzAdvService;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserCouponService;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzAdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 管理端模块
 *
 * @author ruiyifeng
 * @since 2018-04-26
 */
@RestController
@RequestMapping("/wz/api/v3/wzAdmin/")
public class WzAdminApiV3 extends ApiBaseController {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private AsyncService asyncService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private WzAdminServiceImpl adminService;
    @Autowired
    private WzMapActivityMapper activityMapper;
    @Autowired
    private WzCouponMapper couponMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IWzAdvService advService;
    @Autowired
    private IWzMapActivityService mapActivityService;
    @Autowired
    private IWzUserCouponService userCouponService;
    @Autowired
    private WzUserActivityMapper userActivityMapper;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private WzAdminServiceImplV3 adminServiceImplV3;
    @Autowired
    private WzFinanceService financeService;
    @Autowired
    private WzCouponOrderMapper couponOrderMapper;
    @Autowired
    private WzMapActivityMapper mapActivityMapper;
    @Autowired
    private WzAdvMapper advMapper;
    @Autowired
    private WzShareMapper shareMapper;
    @Autowired
    private WzAdvShareMapper advShareMapper;
    @Autowired
    private WzMapMergeMapper mapMergeMapper;
    @Autowired
    private WzUserMergeMapper userMergeMapper;
    @Autowired
    private WzMapRebateMapper mapRebateMapper;
    @Autowired
    private WzCouponMarketMapper couponMarketMapper;

    /**
     * 待审核券库列表
     */
    @PostMapping("couponMarketCheckList")
    public Res couponMarketCheckList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        String sql = "coupon_market_id As couponMarketId,coupon_id As couponId,market_price As marketPrice,total_number As totalNumber";
        List<Map<String, Object>> couponMarkets = couponMarketMapper.selectMapsPage(new Page<>(pageNum, pageSize),new EntityWrapper<WzCouponMarket>().setSqlSelect(sql).eq("status",EnWzCouponMarket.STATUS_CHECKING.getIndex()));
        for(Map<String, Object> map:couponMarkets){
            WzCoupon coupon = cacheService.coupon(Long.parseLong(map.get("couponId").toString()));
            map.put("pic",coupon.getPic());
            map.put("advMapId",coupon.getAdvMapId());
            map.put("benefit",coupon.getBenefit());
            map.put("pic",coupon.getPic());
            map.put("rule",coupon.getRule());
            map.put("beginTime",coupon.getBeginTime());
            map.put("endTime",coupon.getEndTime());
            map.put("price", coupon.getPrice());
            map.put("logo", coupon.getLogo());
            map.put("name", coupon.getMerchantName());
        }
        return Res.success(paramMap.data(couponMarkets));
    }

    /**
     * 券库审核
     */
    @PostMapping("couponMarketCheck")
    public Res couponMarketCheck(@RequestBody ParamMap paramMap) throws Exception {
        Long couponMarketId = paramMap.getLongMisValid("couponMarketId");
        int checkResult = paramMap.getIntMisValid("checkResult");
        String checkRemark = paramMap.getStrMisValid("checkRemark");

        //权限判断
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzUser adminUser = cacheService.user(userCode);
        List<SysUser> sysUser = sysUserMapper.selectList(new EntityWrapper<SysUser>().eq("phone", adminUser.getAccount()).eq("status",1));
        if (sysUser.get(0).getRoleid().equals("10") || sysUser.get(0).getRoleid().equals("7")) {
        }else{
            return Res.fail("没有审核权限!");
        }

        WzCouponMarket couponMarket = couponMarketMapper.selectById(couponMarketId);
        WzCoupon coupon = cacheService.coupon(couponMarket.getCouponId());
        WzUser user = cacheService.user(coupon.getUserCode());
        if (checkResult == EnWzCheckRecord.CHECK_RESULT_SUCCESS.getIndex()) {//审核通过
            //改变状态
            couponMarket.setStatus(EnWzCouponMarket.STATUS_RELEASE.getIndex());
            couponMarket.setRejectRemark("");
            couponMarketMapper.updateById(couponMarket);
            //用户消息
            WzUserMessage userMessage = new WzUserMessage();
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Release.getIndex());//周边店铺相关
            userMessage.setUserCode(coupon.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个返利活动发布成功，请注意查看!");
            userMessage.setTitle("返利活动发布成功");
            asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
            return Res.success();
        } else if (checkResult == EnWzCheckRecord.CHECK_RESULT_FAIL.getIndex()) {//审核驳回
            //改变状态
            couponMarket.setStatus(EnWzCouponMarket.STATUS_REJECT.getIndex());
            couponMarket.setRejectRemark(checkRemark);
            couponMarketMapper.updateById(couponMarket);
            //用户消息
            WzUserMessage userMessage = new WzUserMessage();//用户消息
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Release.getIndex());//周边店铺相关
            userMessage.setUserCode(coupon.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个返利活动审核驳回,驳回原因:" + checkRemark);
            userMessage.setTitle("返利活动审核驳回");
            asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
            return Res.success();
        }
        return Res.fail();
    }


    /**
     * 待审核优惠券页面
     */
    @PostMapping("couponCheckList")
    public Res couponCheckList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        List<Map<String, Object>> couponList = couponMapper.selectMapsPage(new Page<>(pageNum, pageSize), new EntityWrapper<WzCoupon>().eq("status", EnWzPrize.COUPON_STATUS_WaitCheck.getIndex()));
        for(Map coupon:couponList){
            coupon.put("name",coupon.get("merchantName"));
        }
        return Res.success(paramMap.data(couponList));
    }


    /**
     * 优惠券审核
     */
    @PostMapping("couponCheck")
    public Res couponCheck(@RequestBody ParamMap paramMap) throws Exception {
        Long couponId = paramMap.getLongMisValid("couponId");
        int checkResult = paramMap.getIntMisValid("checkResult");
        String checkRemark = paramMap.getStrMisValid("checkRemark");

        //权限判断
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzUser adminUser = cacheService.user(userCode);
        List<SysUser> sysUser = sysUserMapper.selectList(new EntityWrapper<SysUser>().eq("phone", adminUser.getAccount()).eq("status",1));
        if (sysUser.get(0).getRoleid().equals("10") || sysUser.get(0).getRoleid().equals("7")) {
        }else{
            return Res.fail("没有审核权限!");
        }

        WzCoupon coupon=couponMapper.selectById(couponId);
        WzUser user = cacheService.user(coupon.getUserCode());
        if (checkResult == EnWzCheckRecord.CHECK_RESULT_SUCCESS.getIndex()) {//审核通过
            //改变礼券状态
            coupon.setStatus(EnWzPrize.COUPON_STATUS_Release.getIndex());
            //用户消息
            WzUserMessage userMessage = new WzUserMessage();
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Release.getIndex());//地图广告审核
            userMessage.setUserCode(coupon.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个礼券发布成功，请注意查看!");
            userMessage.setTitle("礼券发布成功");
            couponMapper.updateById(coupon);
            asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
            return Res.success();
        } else if (checkResult == EnWzCheckRecord.CHECK_RESULT_FAIL.getIndex()) {//审核驳回
            coupon.setStatus(EnWzPrize.COUPON_STATUS_Reject.getIndex());
            coupon.setRejectRemark(checkRemark);
            WzUserMessage userMessage = new WzUserMessage();//用户消息
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Release.getIndex());//地图广告审核
            userMessage.setUserCode(coupon.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个礼券审核驳回,驳回原因:" + checkRemark);
            userMessage.setTitle("礼券审核驳回");
            couponMapper.updateById(coupon);
            asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
            return Res.success();
        }
        return Res.fail();
    }

    /**
     * 待审核地图活动页面
     */
    @PostMapping("mapActivityCheckList")
    public Res mapActivityCheckList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        String sql = "reject_remark As rejectRemark,jointype,usetype,map_activity_id As mapActivityId,coupon_id AS couponId,adv_map_id AS advMapId,name,logo,need_share_number As needShareNumber,pic,subtype,benefit,join_number As joinNumber,total_number As totalNumber,status,begin_time As beginTime,end_time AS endTime";
        List<Map<String, Object>> activityList = activityMapper.selectMapsPage(new Page<>(pageNum, pageSize), new EntityWrapper<WzMapActivity>().setSqlSelect(sql).eq("status", EnWzMapActivity.STATUS_CHECKING.getIndex()));
        for(Map<String, Object> map:activityList){
            Map<String, Object> coupon = cacheService.couponData(Long.parseLong(map.get("couponId").toString()));
            map.put("rule",coupon.get("rule"));
            map.put("couponType",coupon.get("type"));
            map.put("couponBeginTime",coupon.get("beginTime"));
            map.put("couponEndTime",coupon.get("endTime"));
            WzAdvMap advMap = cacheService.advMap(Long.parseLong(map.get("advMapId").toString()));
            map.put("advMapEndTime",advMap.getEndTime());
            map.put("longitude",advMap.getLongitude());
            map.put("latitude",advMap.getLatitude());
        }
        return Res.success(paramMap.data(activityList));
    }

    /**
     * 地图活动提交审核
     */
    @Transactional
    @PostMapping("mapActivityCheck")
    public Res mapActivityCheck(@RequestBody ParamMap paramMap) throws Exception {
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        int checkResult = paramMap.getIntMisValid("checkResult");
        String checkRemark = paramMap.getStrMisValid("checkRemark");
        WzMapActivity mapActivity = activityMapper.selectById(mapActivityId);
        WzUser user = cacheService.user(mapActivity.getUserCode());
        boolean r = false;
        WzCoupon coupon=couponMapper.selectById(mapActivity.getCouponId());
        if (checkResult == EnWzCheckRecord.CHECK_RESULT_SUCCESS.getIndex()) {//审核通过
            mapActivity.setStatus(EnWzMapActivity.STATUS_RELEASE.getIndex());
            coupon.setStatus(EnWzPrize.COUPON_STATUS_Release.getIndex());
            int f = couponMapper.updateById(coupon);
            if(f > 0){
                f = mapActivityMapper.updateById(mapActivity);
                if(f > 0){
                    WzUserMessage userMessage = new WzUserMessage();//用户消息
                    userMessage.setCreateTime(new Date());
                    userMessage.setType(EnWzUser.Message_Type_AdvMap_Activity_Redpacket.getIndex());//活动
                    userMessage.setUserCode(mapActivity.getUserCode());
                    userMessage.setContent("尊敬的用户，您有一个活动发布成功，请注意查看!");
                    userMessage.setTitle("活动发布成功");
                    asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
                    return Res.success();
                }
            }
            return Res.fail();
        } else if (checkResult == EnWzCheckRecord.CHECK_RESULT_FAIL.getIndex()) {//审核驳回
            mapActivity.setStatus(EnWzMapActivity.STATUS_REJECT.getIndex());
            mapActivity.setRejectRemark(checkRemark);
            if(coupon.getType()== EnWzPrize.TYPE_Shopping_Coupon.getIndex()){//商城券
                List<WzCouponOrder> couponOrderList=couponOrderMapper.selectList(new EntityWrapper<WzCouponOrder>().eq("user_code",mapActivity.getUserCode()).eq("coupon_id",mapActivity.getCouponId()));
                if(couponOrderList.size()>0){
                    WzCouponOrder couponOrder=couponOrderList.get(0);
                    couponOrder.setUsedNumber(couponOrder.getUsedNumber()-mapActivity.getTotalNumber());
                    r = adminServiceImplV3.saveWzMapActivityCheckFailUseShopCoupon(mapActivity,couponOrder);
                }
            }else if(coupon.getType()== EnWzPrize.TYPE_Activity_Coupon.getIndex()){//活动券
                r = financeService.mapActivityCheck(mapActivity,user);
            }
            if(r){
                WzUserMessage userMessage = new WzUserMessage();//用户消息
                userMessage.setCreateTime(new Date());
                userMessage.setType(EnWzUser.Message_Type_AdvMap_Activity_Redpacket.getIndex());//活动审核
                userMessage.setUserCode(mapActivity.getUserCode());
                userMessage.setContent("尊敬的用户，您有一个活动审核驳回,驳回原因:" + checkRemark);
                userMessage.setTitle("活动审核驳回");
                asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
                return Res.success();
            }

        }
        return Res.fail();
    }


    /**
     * 地图活动统计
     */
    @PostMapping("mapActivityStatistics")
    public Res mapActivityStatistics(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        //活动
        EntityWrapper<WzMapActivity> e = new EntityWrapper<WzMapActivity>();
        e.eq("status", EnWzMapActivity.STATUS_RELEASE.getIndex());
        String sql = "reject_remark As rejectRemark,jointype,usetype,map_activity_id As mapActivityId,coupon_id AS couponId,adv_map_id AS advMapId,name,logo,need_share_number As needShareNumber,pic,subtype,benefit,join_number As joinNumber,total_number As totalNumber,status,begin_time As beginTime,end_time AS endTime";
        List<Map<String, Object>> advMapActivityList = mapActivityMapper.selectMapsPage(new Page<>(pageNum,pageSize),e.setSqlSelect(sql));
        for(Map<String, Object> map:advMapActivityList){
            Map<String, Object> coupon = cacheService.couponData(Long.parseLong(map.get("couponId").toString()));
            map.put("rule",coupon.get("rule"));
            map.put("couponType",coupon.get("type"));
            map.put("couponBeginTime",coupon.get("beginTime"));
            map.put("couponEndTime",coupon.get("endTime"));
            WzAdvMap advMap = cacheService.advMap(Long.parseLong(map.get("advMapId").toString()));
            map.put("advMapEndTime",advMap.getEndTime());
            map.put("longitude",advMap.getLongitude());
            map.put("latitude",advMap.getLatitude());
        }
        return Res.success(paramMap.data(advMapActivityList));
    }

    /**
     * 所有广告列表
     */
    @PostMapping("advList")
    public Res advList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        //分享广告
        EntityWrapper<WzAdv> e = new EntityWrapper<WzAdv>();
        e.eq("subtype", EnWzAdv.SUBTYPE_ADV.getIndex()).in("status", new Integer[]{EnWzAdv.STATUS_RELEASE_no.getIndex(), EnWzAdv.STATUS_RELEASE_yes.getIndex(), EnWzAdv.STATUS_Settlement.getIndex()}).orderBy("release_time",false);
        String sql = "share_reward As shareReward,screen_city As screenCity,share_average_reward As shareAverageReward,adv_id As advId,adv_map_id As advMapId,title,avatar_url As logo,nick_name As name,cover_url As coverUrl,subtype,status,total_number As totalNumber,push_number As pushNumber,begin_time As beginTime,end_time As endTime";
        List<Map<String, Object>> advList = advMapper.selectMapsPage(new Page<>(pageNum,pageSize),e.setSqlSelect(sql));
        for (Map<String, Object> map : advList) {
            //参与分享人数
            int shareNumber = advShareMapper.selectCount(new EntityWrapper<WzAdvShare>().eq("adv_id",map.get("advId")));
            map.put("shareNumber",shareNumber);
            //可参与分享总人数
            map.put("shareUserNumber",Integer.parseInt(map.get("shareReward").toString())/Integer.parseInt(map.get("shareAverageReward").toString()));
            map.put("totalshareNumber", "0");//有效点击数
            map.put("totalclickNumber", "0");//总点击数
            List<Map<String, Object>> totalshareNumber = advShareMapper.selectMaps(Condition.create().setSqlSelect("sum(share_number) As totalshareNumber").eq("adv_id", Long.parseLong(map.get("advId").toString())));
            List<Map<String, Object>> totalclickNumber = advShareMapper.selectMaps(Condition.create().setSqlSelect("sum(click_number) As totalclickNumber").eq("adv_id", Long.parseLong(map.get("advId").toString())));
            if (totalshareNumber.get(0) != null) {
                map.put("totalshareNumber", Integer.parseInt(totalshareNumber.get(0).get("totalshareNumber").toString()) + Integer.parseInt(map.get("pushNumber").toString()));
            }
            if (totalclickNumber.get(0) != null) {
                map.put("totalclickNumber", Integer.parseInt(totalclickNumber.get(0).get("totalclickNumber").toString()) + Integer.parseInt(map.get("pushNumber").toString()));
            }
        }
        return Res.success(paramMap.data(advList));
    }

    /**
     * 广告有效点击列表
     */
    @PostMapping("effectivecClickList")
    public Res effectivecClickList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        String advId=paramMap.getStrMisValid("advId");
        String sql = "avatar_url As avatarUrl,nick_name As nickName,create_time As createTime";
        List<Map<String,Object>> effectivecClickList = shareMapper.selectMapsPage(new Page<>(pageNum,pageSize),new EntityWrapper<WzShare>().setSqlSelect(sql).eq("target_id",advId).orderBy("create_time",false));
        return Res.success(paramMap.data(effectivecClickList));
    }

    /**
     * 待审核拼单列表
     */
    @PostMapping("mapMergeCheckList")
    public Res mapMergeCheckList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        //拼单活动列表
        String sql = "limit_number AS limitNumber,total_number AS totalNumber,adv_map_id As advMapId,map_merge_id AS mapMergeId,merchant_name AS merchantName,benefit,merge_price AS mergePrice,sales,pic,begin_time As beginTime,end_time As endTime,coupon_id As couponId";
        List<Map<String, Object>> mergeList = mapMergeMapper.selectMapsPage(new Page<>(pageNum, pageSize), new EntityWrapper<WzMapMerge>().setSqlSelect(sql).eq("status", EnWzMapMerge.STATUS_CHECKING.getIndex()));
        for(Map<String, Object> map : mergeList){
            WzCoupon coupon = cacheService.coupon(Long.parseLong(map.get("couponId").toString()));
            map.put("price",coupon.getPrice());
        }
        return Res.success(paramMap.data(mergeList));
    }

    /**
     * 拼单审核
     */
    @PostMapping("mapMergeCheck")
    public Res mapMergeCheck(@RequestBody ParamMap paramMap) throws Exception {
        Long mapMergeId = paramMap.getLongMisValid("mapMergeId");
        int checkResult = paramMap.getIntMisValid("checkResult");
        String checkRemark = paramMap.getStrMisValid("checkRemark");

        //权限判断
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzUser adminUser = cacheService.user(userCode);
        List<SysUser> sysUser = sysUserMapper.selectList(new EntityWrapper<SysUser>().eq("phone", adminUser.getAccount()).eq("status",1));
        if (sysUser.get(0).getRoleid().equals("10") || sysUser.get(0).getRoleid().equals("7")) {
        }else{
            return Res.fail("没有审核权限!");
        }
        WzMapMerge mapMerge=mapMergeMapper.selectById(mapMergeId);
        WzAdvMap advMap=cacheService.advMap(mapMerge.getAdvMapId());
        WzUser user = cacheService.user(advMap.getUserCode());
        if (checkResult == EnWzCheckRecord.CHECK_RESULT_SUCCESS.getIndex()) {//审核通过
            //改变状态
            mapMerge.setStatus(EnWzMapMerge.STATUS_RELEASE.getIndex());
            mapMerge.setRejectRemark("");
            //用户消息
            WzUserMessage userMessage = new WzUserMessage();
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Coupon.getIndex());//地图广告审核
            userMessage.setUserCode(advMap.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个拼单发布成功，请注意查看!");
            userMessage.setTitle("拼单发布成功");
            boolean r = adminService.mapMergeCheck(advMap,mapMerge);
            if(r){
                asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
                return Res.success();
            }
            return Res.fail("请重新审核!");

        } else if (checkResult == EnWzCheckRecord.CHECK_RESULT_FAIL.getIndex()) {//审核驳回
            mapMerge.setStatus(EnWzMapMerge.STATUS_REJECT.getIndex());
            mapMerge.setRejectRemark(checkRemark);
            WzUserMessage userMessage = new WzUserMessage();//用户消息
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Coupon.getIndex());//地图广告审核
            userMessage.setUserCode(advMap.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个拼单审核驳回,驳回原因:" + checkRemark);
            userMessage.setTitle("拼单审核驳回");
            int r = mapMergeMapper.updateById(mapMerge);
            if(r > 0){
                asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
                return Res.success();
            }
            return Res.fail("请重新审核!");
        }
        return Res.fail();
    }

    /**
     * 待审核返利列表
     */
    @PostMapping("mapRebateCheckList")
    public Res mapRebateCheckList(@RequestBody ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        int type = paramMap.getIntMisValid("type");//1-自己的返利活动；2-券库返利活动；
        //返利活动列表
        String sql = "limit_number As limitNumber,need_share_number As needShareNumber,map_rebate_id As mapRebateId,adv_map_id As advMapId,merchant_name AS merchantName,benefit,rebate_price AS rebatePrice,sales,pic,begin_time As beginTime,end_time As endTime,coupon_id As couponId";
        List<Map<String, Object>> mergeList = mapRebateMapper.selectMapsPage(new Page<>(pageNum, pageSize), new EntityWrapper<WzMapRebate>().setSqlSelect(sql).eq("status", EnWzMapRebate.STATUS_CHECKING.getIndex()).eq("type",type));
        for(Map<String, Object> map : mergeList){
            WzCoupon coupon = cacheService.coupon(Long.parseLong(map.get("couponId").toString()));
            map.put("price",coupon.getPrice());
        }
        return Res.success(paramMap.data(mergeList));
    }

    /**
     * 返利审核
     */
    @PostMapping("mapRebateCheck")
    public Res mapRebateCheck(@RequestBody ParamMap paramMap) throws Exception {
        Long mapRebateId = paramMap.getLongMisValid("mapRebateId");
        int checkResult = paramMap.getIntMisValid("checkResult");
        String checkRemark = paramMap.getStrMisValid("checkRemark");

        //权限判断
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzUser adminUser = cacheService.user(userCode);
        List<SysUser> sysUser = sysUserMapper.selectList(new EntityWrapper<SysUser>().eq("phone", adminUser.getAccount()).eq("status",1));
        if (sysUser.get(0).getRoleid().equals("10") || sysUser.get(0).getRoleid().equals("7")) {
        }else{
            return Res.fail("没有审核权限!");
        }
        WzMapRebate mapRebate = mapRebateMapper.selectById(mapRebateId);
        WzAdvMap advMap=cacheService.advMap(mapRebate.getAdvMapId());
        WzUser user = cacheService.user(advMap.getUserCode());
        if (checkResult == EnWzCheckRecord.CHECK_RESULT_SUCCESS.getIndex()) {//审核通过
            //改变状态
            mapRebate.setStatus(EnWzMapRebate.STATUS_RELEASE.getIndex());
            mapRebate.setRejectRemark("");
            //用户消息
            WzUserMessage userMessage = new WzUserMessage();
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Release.getIndex());//周边店铺相关
            userMessage.setUserCode(advMap.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个返利活动发布成功，请注意查看!");
            userMessage.setTitle("返利活动发布成功");
            boolean r = adminService.mapRebateCheck(advMap,mapRebate);
            if(r){
                asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
                return Res.success();
            }
            return Res.fail("请重新审核!");
        } else if (checkResult == EnWzCheckRecord.CHECK_RESULT_FAIL.getIndex()) {//审核驳回
            //改变状态
            mapRebate.setStatus(EnWzMapRebate.STATUS_REJECT.getIndex());
            mapRebate.setRejectRemark(checkRemark);

            //用户消息
            WzUserMessage userMessage = new WzUserMessage();//用户消息
            userMessage.setCreateTime(new Date());
            userMessage.setType(EnWzUser.Message_Type_AdvMap_Release.getIndex());//周边店铺相关
            userMessage.setUserCode(advMap.getUserCode());
            userMessage.setContent("尊敬的用户，您有一个返利活动审核驳回,驳回原因:" + checkRemark);
            userMessage.setTitle("返利活动审核驳回");
            int r = mapRebateMapper.updateById(mapRebate);
            if(r > 0){
                asyncService.checkUserAdv(userMessage, user, userMessage.getType(), null);
                return Res.success();
            }
            return Res.fail("请重新审核!");
        }
        return Res.fail();
    }
}
